<?php
/**
 * @author Garry
 * this model class gives access to info about a gallery
 *
 */
class GalleryModel extends CI_Model {
	//TODO - Insert your code here


	/**
	 *
	 * @access  public
	 * Constructor
	 */
	public  function __construct() {
		parent::__construct();
		//TODO - Insert your code here
	}
	/**
	 * counts the number of categories that exist
	 * @return int
	 */
	public function countCategory(){
		return $this->db->count_all('category');
	}
	/**
	 * 
	 * Delete this category
	 * @param unknown_type $id
	 */
	public function deleteCategory($id){
		$cat = $this->getCategoryById( $id );
		if( $cat->name == "default" ){
		return FALSE;
		}
		$this->db->delete('category',array('id'=>$id));
		return TRUE;
	}
	/**
	 * counts the number of categories that exist
	 * @return int
	 */
	public function countImagesInCategory( $catID ){
		$query = $this->db->query("SELECT COUNT(*) as num FROM image WHERE category_id = $catID");
		$ans = $query->result();
		return $ans[0]->num;
	} 
	
	/**
	 * Get all categories in the system
	 */
	public function getCategories(){
		return $this->db->get('category' )->result();
	}
	
	public function newCategory( $name ){
		$cat_data = array(
				   'name' => $name 
					);
		$this->db->insert('category', $cat_data );
		return $this->db->insert_id();
	}
	/**
	 * get an individual category
	 * @param int $num
	 * @return object
	 */
	public  function getCategory($num){
		$res = $this->db->get('category',1,$num)->result();
		return $res[0];
	}
	
	/**
	 * get an individual category
	 * @param int $num
	 * @return object
	 */
	public  function getCategoryById($id){
		$retArray = $this->db->get_where('category',array('id'=>$id))->result();
		return $retArray[0];
	}
	/**
	 * Gets all the images from the datbase
	 * @param int $num
	 * @param int $offset
	 * @return array
	 */
	public function getImages($id,$num,$offset){
		return $this->db->get_where('image',array('category_id'=>$id),$num,$offset)->result();
	}
	
	/**
	 * Gets an image from the datbase
	 * @param int $id
	 * @return array
	 */
	public function getImage($id){
		$retArray = $this->db->get_where('image',array('id'=>$id))->result();
		return $retArray[0];
	}
	/**
	 * Insert a row into the image table
	 * @param string $title
	 * @param string $location
	 * @param string $width
	 * @param string $height
	 * @param string $description
	 * @param string $short_desc
	 * @param string $origin_date
	 * @param string $category_id
	 */
	public function newImage( $title ,$location,$width,$height,$description,$short_desc,$origin_date,$category_id ){
		$ins_data = array(
				   'title' => $title,
				   'location' => $location,
				   'width' => $width,
				   'height' => $height, 
				   'description' => $description,
			       'short_desc' => $short_desc,
				   'origin_date' => $origin_date,
				   'category_id' => $category_id
				);
		$this->db->insert('image', $ins_data );
	}
	/**
	 * update a row in the image table
	 * @param string $title
	 * @param string $location
	 * @param string $width
	 * @param string $height
	 * @param string $description
	 * @param string $short_desc
	 * @param string $origin_date
	 * @param string $category_id
	 */
	public function updateImage( $id,$location,$title ,$description,$short_desc,$category_id ){
		$ins_data = array(
				   'location'=>$location,
				   'title' => $title,
				   'description' => $description,
			       'short_desc' => $short_desc,
				   'category_id' => $category_id
				);
		$this->db->update('image', $ins_data,array('id' => $id) );
	}
	
	public function deleteImage($id){
		$this->db->delete('image',array('id'=>$id));
	}
	/**
	 * Checks to see if a gallery of this name already exists
	 * @param unknown_type $name
	 * @return boolean
	 */
	public function checkGalleryExists($name){
		$query = $this->db->get_where('category', array('name'=>$name) );
		if( $query->num_rows() > 0 ){
			return true;
		}
		return false;
	}
}


?>